--
-- Author: yawei.zhu
-- Date: 2018-06-21 14:58:07
--
local NewBoxTipsView = require("app.newBag.views.NewBoxTipsView")

local PvpChampionBottom = class("PvpChampionBottom")

function PvpChampionBottom:ctor(node, parent)
	self._bottomNode = node
	self._parent = parent

	self:initNode()
end

function PvpChampionBottom:initNode()
	local bottomPanel = self._bottomNode:getChildByName("Panel")

	self._loadingBar1 = bottomPanel:findChild("bar_1/LoadingBar")
	self._txtExp1 = bottomPanel:findChild("bar_1/exp")
	self._txtDesc1 = bottomPanel:getChildByName("tlt_1")

	self._loadingBar2 = bottomPanel:findChild("bar_2/LoadingBar")
	self._txtExp2 = bottomPanel:getChildByName("exp")
	self._txtDesc2 = bottomPanel:getChildByName("tlt_2")

	self._bgNumPanel1 = bottomPanel:getChildByName("bg_num")
	self._txtNum1 = self._bgNumPanel1:getChildByName("num")
	self._bgNumPanel1:getChildByName("bg_1"):setVisible(false)
	self._bgNumPanel1:getChildByName("bg"):setVisible(true)

	local panel1 = bottomPanel:getChildByName("Panel_icon")
	self._box1 = panel1:getChildByName("icon")
	self._boxMask1 = panel1:getChildByName("icon_2")
	self._boxMask1:setTouchEnabled(true)
	self._boxOpen1 = panel1:getChildByName("over")
	-- 事件
	display.uiAddClick(self._boxMask1, function(sender, eventType)
		self:_showRareInfo( self._boxMask1, 1) -- 参与
	end)
	display.uiAddClick(self._box1, function(sender, eventType)
		executeFunc(self._onOperate, "openChampionBox", 0)
	end)

	local panel2 = bottomPanel:getChildByName("Panel_icon_0")
	self._box2 = panel2:getChildByName("icon")
	self._boxMask2 = panel2:getChildByName("icon_2")
	self._boxMask2:setTouchEnabled(true)
	self._boxOpen2 = panel2:getChildByName("over")

	self._bgNumPanel2 = bottomPanel:getChildByName("bg_num_0") 
	self._txtNum2 = self._bgNumPanel2:getChildByName("num")
	self._bgNumPanel2:getChildByName("bg_1"):setVisible(false)
	self._bgNumPanel2:getChildByName("bg"):setVisible(true)
	-- 事件
	display.uiAddClick(self._boxMask2, function(sender, eventType)
		self:_showRareInfo(self._boxMask2, 2) -- 胜场
	end)
	display.uiAddClick(self._box2, function(sender, eventType)
		executeFunc(self._onOperate, "openChampionBox", 1)
	end)

	self._timeline = self._bottomNode:getTimeline()
	self._timeline:gotoFrameAndPause(0)
end

function PvpChampionBottom:refresh(seasonInfo)
	if seasonInfo == nil then return end
	self._seasonInfo = seasonInfo


	local function setBoxStatus(index, awardCount, times, needTimes)
		local box = self["_box" .. index]
		local mask = self["_boxMask" .. index]
		local open = self["_boxOpen" .. index]

		local numPanel = self["_bgNumPanel" .. index]
		local txtNum = self["_txtNum" .. index]

		box:setVisible(false)
		mask:setVisible(false)
		open:setVisible(false)
		numPanel:setVisible(false)

		if awardCount > 0 then 
			box:setVisible(true)
			numPanel:setVisible(true)
			txtNum:setString(awardCount)
		else
			if times >= needTimes then
				open:setVisible(true)
			else
				mask:setVisible(true)
			end
		end
	end

	local needCount1 = tonumber( GD:getThronePar("throneJoinNum") ) -- 参与次数
	local needCount2 = tonumber( GD:getThronePar("throneWinNum") ) -- 胜利次数
	-- 播宝箱动画
	if seasonInfo.awardPartCount > 0 or seasonInfo.awardWonCount > 0 then
		self._timeline:gotoFrameAndPlay(0, true)
	else
		self._timeline:gotoFrameAndPause(0)
	end
	-- 设置宝箱状态
	setBoxStatus(1, seasonInfo.awardPartCount, seasonInfo.parcipatateTimes, needCount1)
	setBoxStatus(2, seasonInfo.awardWonCount, seasonInfo.wonTimes, needCount2)


	self._txtDesc1:setString(string.format(L("champion_challge1"), needCount1))
	self._txtExp1:setString(seasonInfo.parcipatateTimes .. "/" .. needCount1)
	self._loadingBar1:setPercent(seasonInfo.parcipatateTimes * 100 / needCount1)

	self._txtDesc2:setString(string.format(L("champion_challge2"), needCount2))
	self._txtExp2:setString(seasonInfo.wonTimes .. "/" .. needCount2)
	self._loadingBar2:setPercent(seasonInfo.wonTimes * 100 / needCount2)
end

function PvpChampionBottom:_showRareInfo(triggerNode, type)
	local dataTemp = {}

	dataTemp.name = L("champion_award3")
	dataTemp.notice = L("tips_3000413")
	local key = "throneJoinReward"
	if type == 2 then
		key = "throneWinReward"
	end
	local dropId = GD:getThronePar(key)
	dataTemp.rewards = UD:parseDropIDToBagItem(dropId)

	local rarePanel = NewBoxTipsView.create()
	rarePanel:setData(dataTemp)
	UIHandler:addPopupUi(rarePanel)

    -- 点击空白区域关闭
	display.nodeRemoveForNotHit(rarePanel, function()
		rarePanel:removeFromParent()
	end)

	local box = triggerNode:getBoundingBox()
	local worldPos = triggerNode:convertToWorldSpace( cc.p(0, 0) )
	box.x = worldPos.x
	box.y = worldPos.y

	display.adjustNodePos(box, rarePanel)
end

function PvpChampionBottom:onOperate(cb)
	self._onOperate = cb
end


return PvpChampionBottom
